var app = angular.module('index', []);
 app.controller('IndexController', function($scope) {
                $scope.step = 0;
                $scope.next=function(){
                    $scope.step++;
                };
               
                $scope.reset=function(){
                    $scope.step = 0;
                };
                $scope.$on('reset',function(){
                    $('#booked-step').removeClass('active');
                    $scope.reset();
                   
                });
                $scope.back=function(){
                    $scope.step--;
                };
                $scope.finish=function(){
                    $scope.step=3;
                };
    });
app.directive('reset', function() {
    return {
        restrict: 'AE',
        controller: [
            '$scope', '$element', '$attrs', '$parse', function($scope, $element, $attrs, $parse) {
               $scope.$broadcast('reset');
            }
        ]
    };
});
app.directive('ngInitial', function() {
    return {
        restrict: 'A',
        controller: [
            '$scope', '$element', '$attrs', '$parse', function($scope, $element, $attrs, $parse) {
                var getter, setter, val;
                val = $attrs.ngInitial || $attrs.value || $element.html();
                getter = $parse($attrs.ngModel);
                setter = getter.assign;
                setter($scope, val);
            }
        ]
    };
});
app.directive('ngSelection', function() {
    return {
        restrict: 'A',
        controller: [
            '$scope', '$element', '$attrs', '$parse', function($scope, $element, $attrs, $parse) {
                $element.bind('mouseup', function(e) {
                    var html = "";
                    if (window.getSelection) {
                        html = window.getSelection().toString();
                    } else if (document.selection && document.selection.type != "Control") {
                        html = document.selection.createRange().text;
                    }
                    if (html) {
                        $scope.showEditor(e);
                    }
                });
            }
        ]
    };
});
app.directive('ngFilereader', function() {
    return {
        restrict: 'A',
        controller: [
            '$scope', '$element', '$attrs', '$parse', function($scope, $element, $attrs, $parse) {
                $element.bind("change", function(event) {
                    if ($element[0].files && $element[0].files[0]) {
                            var reader = new FileReader();

                            reader.onload = function(e) {
                                $scope.src=e.target.result;
                                $scope.$apply();
                            }
                            reader.readAsDataURL($element[0].files[0]);
                        


                    }
                });
            }
        ]
    };
});
app.directive('maptool', function() {
    return {
        restrict: 'AE',
        controller: [
            '$scope', '$element', '$attrs', '$parse', '$window', function($scope, $element, $attrs, $parse, $window) {
                $scope.poss = APP.seats;
                $scope.booked_seats = APP.booked_seats;
                $scope.src = APP.src||null;
                $scope.active = [];
                $scope.booked = [];
                $scope.selected = [];
                $scope.selected_name = [];
                $scope.edit = null;
                
                $scope.posAtts = {
                    width: 30,
                    height: 20,
                    margin: 10
                };
                $scope.selectedType=[
                   [],[],[],[],[]
                ];
                $scope.selected_count=0;
                
                $scope.getTotalPrice=function() {
                    total = 0;
                    $('.ticket-type').each(function () {
                        if ($(this).is(":checked")) {
                            price = $(this).attr('data-price');
                            quantity = $(this).parent().parent().find(".select-ticket-number").get(0).value;

                            total += price * quantity;
                        }
                    })
                    return number_format(total) + " VND";
                }
                $scope.getTotalPrice2=function() {
                    total = 0;
                    $('.select-ticket-number').each(function () {
                            price = $(this).attr('data-price');
                            quantity = $(this).get(0).value;
                            total += price * quantity;
                    })
                    return total.toLocaleString().replace('.',',') + " VND";
                }
                $scope.activePos = function($index) {
                    if ($scope.booked_seats.indexOf($index)!== -1) {
                        return ;
                    }  
                    var activePos =$scope.poss[$index];
                    if ( $scope.selected.indexOf($index)!== -1) {
                        $scope.selected.splice( $scope.selected.indexOf($index),1);
                        $scope.selectedType[activePos.type].splice($scope.selectedType.indexOf($index),1);
                        $scope.selected_count--;
                        return;
                    }       
                    if ($scope.selected_count ===10) {
                        return;
                    }
                    $scope.selectedType[activePos.type].push($index);
                    $scope.selected.push($index);
                    $scope.selected_count++;
                    //$scope.selected.push($scope.poss[$index].name);
                    
                };
                $scope.haveFile = function() {
                    if ($scope.src) {
                        return true;
                    }
                    return false;
                };
                $scope.bookedClass=function($index){
                    if ($scope.booked_seats.indexOf($index)!== -1) {
                        return 'booked';
                    }  
                    return '';
                };
                $scope.activeClass = function($index) {
                    if ($scope.selected.indexOf($index) !== -1) {

                        return 'selected';
                    }
                    return '';
                };
                $scope.isEdit = function($index) {
                    if ($scope.edit === $index) {
                        return 'true';
                    }
                    ;
                    return 'false';
                };
                $scope.setEdit = function($index) {
                    return  $scope.edit = $index;
                };
                $scope.getSeats = function($index) {
                    return JSON.stringify($scope.poss);
                };
                $scope.getSelectedType = function($index) {
                    return JSON.stringify($scope.selectedType);
                };
                $scope.getSelected = function($index) {
                    return JSON.stringify($scope.selected);
                };
                $scope.getSeatsOfType = function($type){
                    $result=[];
                    for($i in $scope.selected){
                        $seat=$scope.poss[$scope.selected[$i]];
                        console.log($seat);
                        if ($seat.type==$type) {
                             console.log($seat);
                             $result.push($seat.name);
                        }
                    }
                    return $result.join(',');
                };
                
            }
        ]
    };
});
app.directive('preventclick', function() {
    return {
        restrict: 'AE',
        controller: [
            '$scope', '$element', '$attrs', '$parse', function($scope, $element, $attrs, $parse) {
                $element.bind('click', function(e) {
                    e.preventDefault();
                    e.stopPropagation();
                    return false;
                });
            }
        ]
    };
});
app.directive('pos', function() {
    return {
        restrict: 'AE',
        controller: [
            '$scope', '$element', '$attrs', '$parse', function($scope, $element, $attrs, $parse) {
                $element.bind('keypress', function(e) {
                    var $keycode = window.event.keyCode;
                    if ($keycode === 13) {
                        $element[0].blur();
                        e.preventDefault();
                        e.stopPropagation();
                    }
                    if ($element.text().length === 3) {
                        e.preventDefault();
                        e.stopPropagation();
                    }
                    return ;
                });


            }
        ]
    };
});